using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Reflection;
using System.Text;
using System.Windows.Forms;
using YamahaTuanTruc.Controls;
using Qios.DevSuite.Components;

namespace YamahaTuanTruc
{
    public partial class FrmMain : Qios.DevSuite.Components.Ribbon.QRibbonForm
    {
        /// <summary>
        /// Contains the static statusPanel so that other windows can reach it.
        /// </summary>
        public static QStatusBarPanel StatusPanel;

        /// <summary>
        /// Contains the time counter for when the statusbar should get reset.
        /// </summary>
        public static int MouseOffMenuItem = -1;

        /// <summary>
        /// Contains the time counter for when the statusbar should get reset.
        /// </summary>
        private int m_iStatusBarRefreshTime = 200;
        private int m_iStatusBarLastMessageTime = 2000;
        private Queue m_oStatusBarQueue;
        private int m_iLastMessageShownTime = -1;

        /// <summary>
        /// The animation bitmap for the statusbar
        /// </summary>
        private Bitmap m_oAnimationBitmap;

        /// <summary>
        /// The progress bar for the statusbar
        /// </summary>
        private QProgressBar m_oProgressBar;

        /// <summary>
        /// The state manager of application
        /// </summary>
        private Panel m_oAppStateImageContainer;

        public FrmMain()
        {
            InitializeComponent();
            InitializeNavigator();
            // Create a queue for displaying events
            m_oStatusBarQueue = new System.Collections.Queue();
            // Set the interval for the timer
            tmrTimedRetrievals.Interval = m_iStatusBarRefreshTime;
            // Retrieve the animation bitmap for the statusbar
            m_oAnimationBitmap = new Bitmap(System.Reflection.Assembly.GetCallingAssembly().GetManifestResourceStream("YamahaTuanTruc.Images.QActionAni.gif"));

            // Create a progressbar for the status bar
            m_oProgressBar = new QProgressBar();
            m_oProgressBar.BlockSize = 6;
            m_oProgressBar.BlockMargin = 1;
            m_oProgressBar.Dock = DockStyle.Fill;
            m_oProgressBar.Location = new Point(200, 10);
            m_oProgressBar.Size = new Size(50, 16);
            m_oProgressBar.MaxValue = 10;

            qsbProgressBar.Control = m_oProgressBar;

            // Create a status panel for the statusbar
            m_oAppStateImageContainer = new Panel();
            m_oAppStateImageContainer.Size = new Size(60, 16);
            m_oAppStateImageContainer.Dock = DockStyle.Right;
            m_oAppStateImageContainer.BackColor = Color.Transparent;

            qsbStatus.Control = m_oAppStateImageContainer;

            qsiAppState.StateImagesContainer = m_oAppStateImageContainer;
            qsiAppState.MainProgressBar = m_oProgressBar;
            qsiAppState.MainStatusBarPanel = qsbStatus;

            qsbDate.Text = DateTime.Now.ToLongDateString();
            qsbUser.Text = Program.User.Username;

            qsiAppState.SetState("RetrievalState", "Downloading...", "Downloading...", qsiAppState["RetrievalState"] + 5, QAppStateIndicatorType.AllInMain, null);

            FrmMain.StatusPanel = qsbStatus;

            //show the first form
            bool selectedItem = false;
            foreach (QMenuItem item in this.qebNavigator.ExplorerItems)
            {
                if (item.MenuItems.Count > 0)
                {
                    qebNavigator.SetExpandedItem(item, true, true, QMenuItemActivationType.Mouse);
                    foreach (QMenuItem child in item.MenuItems)
                    {
                        if (!selectedItem && child is CustomExplorerItem)
                        {
                            qebNavigator_MenuItemActivated(child, new QMenuEventArgs(child, QMenuItemActivationType.Mouse, false));
                            selectedItem = true;
                        }
                    }
                }
            }
        }

        private void InitializeNavigator()
        {
            qeiByMonth.OwnedForm = new FrmBusinessStatistics();
            qeiByQuarter.OwnedForm = new FrmBussinessStatisticbyQuarter();
            qeiPartsStatistics.OwnedForm = new FrmPartStatistics();
            qeiMaintenance.OwnedForm = new FrmMaintenance();
            qeiMaintenanceList.OwnedForm = null;
            qeiManageCustomer.OwnedForm = new FrmCustomer();
            qeiManageBike.OwnedForm = new FrmBike();
            qeiManagePart.OwnedForm = new FrmPart();
            qeiPartList.OwnedForm = new FrmPartStatistics();
        }

        private void tmrTimedRetrievals_Tick(object sender, EventArgs e)
        {
            tmrTimedRetrievals.Interval = System.Convert.ToInt32(Math.Max(10, m_iStatusBarRefreshTime - ((m_oStatusBarQueue.Count / 10.0) * m_iStatusBarRefreshTime)));
            // Update the caplock and numlock panels
            SynchronizeKeyBoardState();

            // Show the current item in the queue
            if (m_oStatusBarQueue.Count > 0)
            {
                string tmp_sCurrentMessage = System.Convert.ToString(m_oStatusBarQueue.Dequeue());
                qsbStatus.Text = tmp_sCurrentMessage;
                if (m_oStatusBarQueue.Count == 0)
                {
                    m_iLastMessageShownTime = Environment.TickCount;
                }
            }
            else if ((m_iLastMessageShownTime > 0) && ((m_iLastMessageShownTime + m_iStatusBarLastMessageTime) < Environment.TickCount))
            {
                m_iLastMessageShownTime = -1;
                qsbStatus.Text = "";
            }
        }

        /// <summary>
        /// Updates capslock and numlock panels
        /// </summary>
        private void SynchronizeKeyBoardState()
        {
            // Enabled the capslock qstatusbarpanel when capslock is on
            if (!qsbCapsLock.IsDisposed)
            {
                qsbCapsLock.Enabled = Control.IsKeyLocked(Keys.CapsLock);
            }

            // Enabled the numlock qstatusbarpanel when numlock is on
            if (!qsbNumLock.IsDisposed)
            {
                qsbNumLock.Enabled = Control.IsKeyLocked(Keys.NumLock);
            }
        }

        /// <summary>
        /// Sets the statusbar text
        /// </summary>
        /// <param name="e"></param>
        /// <param name="action"></param>
        public void SetStatusBar(QMenuEventArgs e, string action)
        {
            // If there was a selected menuItem
            if (e.MenuItem == null || e.MenuItem.IsSeparator)
            {
                return;
            }

            string tmp_sTitle;
            if (!string.IsNullOrEmpty(e.MenuItem.Title))
            {
                tmp_sTitle = (e.MenuItem.Title.Replace("&", ""));
            }
            else
            {
                tmp_sTitle = "(no title)";
            }

            // Set the text to the selected item.
            string tmp_sText = "Item " + tmp_sTitle + " " + action + " via " + e.ActivationType.ToString();
            m_oStatusBarQueue.Enqueue(tmp_sText);
        }

        private void FrmMain_FormClosing(object sender, FormClosingEventArgs e)
        {
        }

        private void FrmMain_FormClosed(object sender, FormClosedEventArgs e)
        {
            Logout();
            Application.Exit();
        }

        private void Logout()
        {
            //throw new NotImplementedException();
        }

        private void FrmMain_Load(object sender, EventArgs e)
        {

        }

        private void qebNavigator_MenuItemActivated(object sender, QMenuEventArgs e)
        {
            if (e.MenuItem is CustomExplorerItem)
            {
                CustomExplorerItem cei = e.MenuItem as CustomExplorerItem;

                string img = "YamahaTuanTruc.Images.";
                img += string.IsNullOrEmpty(cei.LargeImage) ? "yamaha-logo-title.png" : cei.LargeImage;
                this.panelTitle.BackgroundImage = new Bitmap(System.Reflection.Assembly.GetExecutingAssembly().GetManifestResourceStream(img));

                this.lblTitle.Text = string.IsNullOrEmpty(cei.LongTitle) ? cei.Title : cei.LongTitle;
                this.lblDescription.Text = string.IsNullOrEmpty(cei.Description) ? "" : cei.Description;

                Form tmp_oForm = cei.OwnedForm;
                if (tmp_oForm != null)
                {
                    tmp_oForm.WindowState = FormWindowState.Maximized;
                    tmp_oForm.MdiParent = this;
                    tmp_oForm.Show();
                }
            }
        }
    }
}